package com.originskyseed.platform.cms.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsAnchorRelateCollect;
import com.originskyseed.platform.cms.service.ICmsAnchorRelateCollectService;
import com.originskyseed.platform.cms.vo.request.CmsAnchorRelateCollectRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 收藏主播信息表Controller类（OSI_ANCHOR_RELATE_COLLECT）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/cmsAnchorRelateCollect")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsAnchorRelateCollectController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsAnchorRelateCollectController.class);

	@DubboReference
	private ICmsAnchorRelateCollectService cmsAnchorRelateCollectService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsAnchorRelateCollectList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<CmsAnchorRelateCollect>> cmsAnchorRelateCollectList(HttpServletRequest request) {
		BaseResponseBody<BasePage<CmsAnchorRelateCollect>> result = null;
		try {
			// 请求对象
			CmsAnchorRelateCollectRequestVO requestVO = this.getParameterBean(CmsAnchorRelateCollectRequestVO.class);

			// 分页查询
			BasePage<CmsAnchorRelateCollect> page = new BasePage<CmsAnchorRelateCollect>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsAnchorRelateCollectService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<CmsAnchorRelateCollect>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_ANCHOR_RELATE_COLLECT_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<CmsAnchorRelateCollect>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_ANCHOR_RELATE_COLLECT_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 业务员_收藏/取消主播
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsAnchorRelateCollectSave.json")
	@RequiredParams(names = { "type", "anchorId" })
	public BaseResponseBody cmsAnchorRelateCollectSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsAnchorRelateCollect cmsAnchorRelateCollect = new CmsAnchorRelateCollect();
			String anchorId = this.getParameterString("anchorId", null);
			Integer type = this.getParameterInteger("type", null);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (StringUtils.isNotBlank(anchorId) && type != null) {
				CmsAnchorRelateCollect collect = new CmsAnchorRelateCollect();
				collect.setAnchorId(anchorId);
				collect.setBusinessId(userInfo.getId());
				CmsAnchorRelateCollect anchorRelateCollect = this.cmsAnchorRelateCollectService.selectCollectByAnchorIdAndUserId(collect);
				if (anchorRelateCollect != null) {
					cmsAnchorRelateCollect.setIsDel(type);// 取消收藏
					cmsAnchorRelateCollect.setBusinessId(userInfo.getId());
					cmsAnchorRelateCollect.setAnchorId(anchorId);
					cmsAnchorRelateCollect.setId(anchorRelateCollect.getId());
					this.cmsAnchorRelateCollectService.updateByPrimaryKeySelective(cmsAnchorRelateCollect);
				} else {
					cmsAnchorRelateCollect.setIsDel(type);
					cmsAnchorRelateCollect.setBusinessId(userInfo.getId());
					cmsAnchorRelateCollect.setAnchorId(anchorId);
					this.cmsAnchorRelateCollectService.insertSelective(cmsAnchorRelateCollect);
				}
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_CONTENT_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsAnchorRelateCollectDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> cmsAnchorRelateCollectDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsAnchorRelateCollectService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
